package com.novel.frame.lgw.Reptile;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test {
//	书库小说图片====免费系列
	public static List<String> img() {
		List<String> listImg = new ArrayList<String>();
		for (int i = 1; i <= 1;) {
			Connection conn = Jsoup.connect("http://www.shuhai.com/shuku/0_0_0_0_0_0_1_" + i + ".html");
			try {
				Document doc = conn.get();
				Elements elesImg = doc.select("div[class=book-list-wrapper]").select("a").select("img");
				for (Element element : elesImg) {
					String Img = element.attr("src");
					listImg.add(Img);
				}
				i++;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return listImg;
	}

//	书库小说名====免费系列
	public static List<String> name() {
		List<String> listName = new ArrayList<String>();
		for (int i = 1; i <= 1;) {
			Connection conn = Jsoup.connect("http://www.shuhai.com/shuku/0_0_0_0_0_0_1_" + i + ".html");
			try {
				Document doc = conn.get();
				Elements elesName = doc.select("div[class=book-list-wrapper]").select("a");
				for (Element element : elesName) {
					String name = element.text();
					if (!"".equals(name)) {
						listName.add(name);
					}
				}
				i++;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return listName;
	}

//	书库小说简介====免费系列
	public static List<String> synopsis() {
		List<String> listSynopsis = new ArrayList<String>();
		for (int i = 1; i <= 1;) {
			Connection conn = Jsoup.connect("http://www.shuhai.com/shuku/0_0_0_0_0_0_1_" + i + ".html");
			try {
				Document doc = conn.get();
				Elements elesTest = doc.select("div[class=book-list-wrapper]").select("div [class=book-intro cl6e]");
				for (Element element : elesTest) {
					listSynopsis.add(element.text());
				}
				i++;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return listSynopsis;
	}
	
//	书库小说类型=====男生频道====免费系列
	public static List<String> type() {
		List<String> listType = new ArrayList<String>();
		for (int i = 1; i <= 1;) {
		Connection conn = Jsoup.connect("http://www.shuhai.com/shuku/0_0_0_0_0_0_1_"+i+".html");
		try {
			Document doc = conn.get();
			Elements eless = doc.select("div[class=book-list-wrapper]").select("span");
			for (Element element : eless) {
				String str = element.attr("class");
				if (str.equals("boy tag")) {
					listType.add(element.text());
				}
			}
			i++;
		} catch (IOException e) {
			e.printStackTrace();
		 }
		}
		return listType;
	}

//	书库小说总字数====免费系列
	public static List<String> wordNum() {
		List<String> listword = new ArrayList<String>();
		for (int i = 1; i <= 1;) {
		Connection conn = Jsoup.connect("http://www.shuhai.com/shuku/0_0_0_0_0_0_1_"+i+".html");
		try {
			Document doc = conn.get();
			Elements eless = doc.select("div[class=book-list-wrapper]").select("span");
			for (Element element : eless) {
				String str = element.attr("class");
				if (str.equals("size clmain")) {
					listword.add(element.text());
				}
			}
			i++;
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
		return listword;
	}

//	书库小说更新时间====免费系列
	public static List<String> updateTime() {
		List<String> list = new ArrayList<String>();
		for (int i = 1; i <= 1;) {
		Connection conn = Jsoup.connect("http://www.shuhai.com/shuku/0_0_0_0_0_0_1_"+i+".html");
		try {
			Document doc = conn.get();
			Elements eless = doc.select("div[class=book-list-wrapper]").select("span");
			for (Element element : eless) {
				String str = element.attr("class");
				if (str.equals("time")) {
					list.add(element.text());
				}
			}
			i++;
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
		return list;
	}

//	书库小说状态====免费系列
	public static List<String> state() {
		List<String> list = new ArrayList<String>();
		for (int i = 1; i <= 1;) {
		Connection conn = Jsoup.connect("http://www.shuhai.com/shuku/0_0_0_0_0_0_1_"+i+".html");
		try {
			Document doc = conn.get();
			Elements eless = doc.select("div[class=book-list-wrapper]").select("span");
			for (Element element : eless) {
				String str = element.attr("class");
				if (str.equals("state")) {
					list.add(element.text());
				}
			}
			i++;
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
		return list;
	}
    
	

	public static void main(String[] args) {			
		Test.download(Test.name(), Test.img()); // 小说图片
		Test.synopsis();// 小说简介
		Test.state();// 小说状态
		Test.updateTime();// 小说更新时间
		Test.wordNum(); // 小说总字数     
		 System.out.println(Test.state());
	}
	

				
   //下载小说图片
	public static void download(List<String> listName, List<String> listImg) {
		for (int i = 0; i < listName.size();) {
			try {
				// 3.1 将这个地址交给URL对象处理
				URL url = new URL(listImg.get(i));
				// 3.2 打开这个连接，并得到一个连接对象
				URLConnection urlconn = url.openConnection();
				// 3.3 获取文件的输入流。
				// 输入：InputStream 用来读取文件
				// 输出：OutputStream 写入文件
				InputStream inputStream = urlconn.getInputStream();
				// 3.4 读取文件信息
				int len = 0;
				// byte:字节。每次读取4kb
				byte[] b = new byte[1024 * 4];
				// inputStream.read(b)；每次读取b数组的长度，4kb，len，存储每次读取的真实长度。
				File file = new File("E://demo/书库图片/" + listName.get(i) + ".png");
				if (file.exists()) {
//					System.out.println(listName.get(i)+"存在");
					i++;
				} else {
					OutputStream out = new FileOutputStream(file);
					while ((len = inputStream.read(b)) != -1) {
						out.write(b, 0, len);
					}
					out.close();
					inputStream.close();
					ImageIO.write(makeRoundedCorner(ImageIO.read(file), 50),
							"png", file); // 将其保存在C:/imageSort/targetPIC/下
					System.out.println("下载完成........下载路径是====>"+"D://demo/"+listName.get(i));
					i++;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 将图片变成圆角
	public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
		int w = image.getWidth();
		int h = image.getHeight();
		BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = output.createGraphics();
		g2.setComposite(AlphaComposite.Src);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(Color.WHITE);
		g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
		g2.setComposite(AlphaComposite.SrcAtop);
		g2.drawImage(image, 0, 0, null);
		g2.dispose();
		return output;
	}

}
